<template>
  <el-dialog
    :title="addDeptsForm.id ? '编辑部门' : '新增部门'"
    :visible="visible"
    width="50%"
    @close="onDialogClose"
  >

    <!-- 绑定整个表单区域 -->
    <el-form ref="form" :model="addDeptsForm" :rules="addDeptsRormRulse" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="addDeptsForm.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="addDeptsForm.code" placeholder="请输入部门编码名称" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="addDeptsForm.manager" placeholder="请选择负责人" @focus="getDeptsManager">
          <el-option
            v-for="item in deptsManager"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="addDeptsForm.introduce"
          type="textarea"
          :rows="2"
          placeholder="请输入部门介绍名称"
        />
        <!-- <el-input v-model="addDeptsForm.introduce" placeholder="请输入部门介绍名称" /> -->
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmplayeesListSimple } from '@/api/emplayees.js'
import { getDepartmentsList, addDepartments, getDepartmentsBYId, editDepartmentsBYId } from '@/api/departments.js'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 校验部门名称
    const validDeptName = async(rule, value, callback) => {
      let flag = false
      if (this.addDeptsForm.id) {
        // 编辑
        // 获取到所有的部门
        const res = await getDepartmentsList()
        // 筛选到同级部门，并且排除自己
        const newArr = res.depts.filter(item => item.pid && item.id !== this.node.id)
        // 根据同级部门是否有重名
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }
      if (flag) {
        callback('不允许名称为' + value)
      } else {
        callback()
      }
    }

    // 校验部门编码是否存在
    const validDeptCode = async(rule, value, callback) => {
      // 返回的数据list 不是tree
      const res = await getDepartmentsList()
      let flag = false
      if (this.addDeptsForm.id) {
        // 编辑
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        flag = res.depts.some(item => item.code === value)
      }
      if (flag) return callback('不允许名称为' + value)
      callback()
    }
    return {
      addDeptsForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      addDeptsRormRulse: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称 1 到 10 个字符', trigger: 'blur' },
          { validator: validDeptName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '请输入部门编码名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码名称 1 到 10 个字符', trigger: 'blur' },
          { validator: validDeptCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人名称', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门介绍名称 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      deptsManager: []
    }
  },

  created() {

  },

  methods: {
    // 关闭对话框
    onDialogClose() {
      this.$emit('input', false)
      // 对表单进行重置
      this.$refs.form.resetFields()
      this.addDeptsForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    // 部门负责人获取焦点
    async getDeptsManager() {
      const res = await getEmplayeesListSimple()
      this.deptsManager = res
    },

    // 点击确定
    async onSave() {
      //
      try {
        await this.$refs.form.validate()
        // 编辑
        if (this.addDeptsForm.id) {
          await editDepartmentsBYId(this.addDeptsForm)
          this.$message.success('编辑成功')
          this.$emit('edit-success')
        } else {
        // 新增
          // 请求的pid 使用node.id
          this.addDeptsForm.pid = this.node.id
          await addDepartments(this.addDeptsForm)
          this.$message.success('添加成功')
          this.$emit('add-success')
        }
      } catch (err) {
        console.log(err)
      }
      this.onDialogClose()
    },
    // 获取最新的数据
    async getDeptsInfo(id) {
      console.log('最新', id)
      const res = await getDepartmentsBYId(id)
      this.addDeptsForm = res
    }

  }
}
</script>

<style scoped lang='less'>

</style>
